import { decryptRes, dealPagination } from '../utils/requestUtil';
import * as pointService from '../services/pointService';
import { message } from 'antd';

export default {
  namespace: 'category',

  state: {
   categoryList: [],
  },

  effects: {
    *fetchCategoryList({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(pointService.fetchCategoryList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'categoryListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *addCategory({ payload, callback }, { call, put }) {
      console.log('addCategory前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(pointService.addCategory, payload)));
      console.log('addCategory接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;
        message.success('添加成功！')
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *editCategory({ payload, callback }, { call, put }) {
      console.log('editCategory', payload)
      const response = JSON.parse(decryptRes(yield call(pointService.editCategory, payload)));
      console.log('editCategory', response);

      if (response.code === 0) {
        let data = response.data;
        message.success('修改成功！')
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *deleteCategory({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(pointService.deleteCategory, payload)));
      if (response.code === 0) {
        let data = response.data;
        message.success('删除成功！')
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

  },

  reducers: {
    categoryListReducer(state, action) {
      return {
        ...state,
        categoryList: action.payload.data,
      };
    },
  },
};
